package com.zhkj.txg.module.mine.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.zhkj.lib.base.BaseViewModel
import com.zhkj.lib.http.BaseResponse
import com.zhkj.lib.http.HttpResponse
import com.zhkj.lib.http.ViewModelSubscribeListener
import com.zhkj.lib.utils.AccountManager
import com.zhkj.lib.utils.RxBus
import com.zhkj.txg.module.home.entity.RechargeListResponse
import com.zhkj.txg.module.mine.entity.BalanceListResponse
import com.zhkj.txg.module.mine.entity.UserCenterResponse
import com.zhkj.txg.module.mine.entity.WithdrawConfigResponse
import com.zhkj.txg.module.mine.model.MineManager
import com.zhkj.txg.module.order.entity.OrderPayResponse
import com.zhkj.txg.utils.PayResult
import com.zhkj.txg.utils.ThirdLoginEvent
import io.reactivex.android.schedulers.AndroidSchedulers

class BalanceViewModel(application: Application) : BaseViewModel(application) {
    private val manager = MineManager()
    private val payResultLiveData = MutableLiveData<PayResult>()
    private val weChatResultLiveData = MutableLiveData<ThirdLoginEvent>()

    val balanceListLiveData = MutableLiveData<HttpResponse<BalanceListResponse>>()
    val rechargeLiveData = MutableLiveData<HttpResponse<OrderPayResponse>>()
    val withdrawConfigLiveData = MutableLiveData<HttpResponse<WithdrawConfigResponse>>()
    val withdrawLiveData = MutableLiveData<HttpResponse<BaseResponse>>()
    val balanceLiveData = MutableLiveData<HttpResponse<UserCenterResponse>>()
    val bindWeChatLiveData = MutableLiveData<HttpResponse<BaseResponse>>()
    val bindAliPayLiveData = MutableLiveData<HttpResponse<BaseResponse>>()

    val rechargeListLiveData = MutableLiveData<HttpResponse<RechargeListResponse>>()
    val recharge2LiveData = MutableLiveData<HttpResponse<BaseResponse>>()

    private var numb = 20
    var page = 1
        private set

    fun refreshBalance(loadLog: Boolean = false) {
        val listener = object : ViewModelSubscribeListener<UserCenterResponse>(balanceLiveData) {
            override fun onSuccess(resp: UserCenterResponse) {
                super.onSuccess(resp)
                if (loadLog) balanceList(AccountManager.getToken(), true)
            }
        }
        val userCenter = manager.userCenter(AccountManager.getToken(), listener)
        dispose.add(userCenter)
    }

    fun balanceList(token: String, restart: Boolean = false) {
        val listener =
            object : ViewModelSubscribeListener<BalanceListResponse>(balanceListLiveData) {
                override fun onSuccess(resp: BalanceListResponse) {
                    resp.hasMore = resp.data.size >= numb
                    resp.loadMore = page > 1
                    super.onSuccess(resp)
                    page += 1
                }
            }
        if (restart) page = 1
        val balanceList = manager.balanceList(token, page, numb, listener)
        dispose.add(balanceList)
    }

    fun recharge(token: String, money: String, payType: String, shopId: String) {
        val listener = ViewModelSubscribeListener(rechargeLiveData)
        val recharge = manager.recharge(token, money, payType, shopId, listener)
        dispose.add(recharge)
    }

    fun withdrawConfig() {
        val listener = ViewModelSubscribeListener(withdrawConfigLiveData)
        val withdrawConfig = manager.withdrawConfig(AccountManager.getToken(), listener)
        dispose.add(withdrawConfig)
    }

    fun withdraw(money: String, type: String, withdrawToken: String) {
        val listener = ViewModelSubscribeListener(withdrawLiveData)
        val withdraw = manager.withdraw(AccountManager.getToken(), money, type, withdrawToken, listener)
        dispose.add(withdraw)
    }

    fun bindWeChat(code: String) {
        val listener = ViewModelSubscribeListener(bindWeChatLiveData)
        val bindWeChat = manager.bindWeChat(AccountManager.getToken(), code, listener)
        dispose.add(bindWeChat)
    }

    fun bindAliPay(name: String, account: String) {
        val listener = ViewModelSubscribeListener(bindAliPayLiveData)
        val bindAliPay = manager.bindAliPay(AccountManager.getToken(), name, account, listener)
        dispose.add(bindAliPay)
    }

    fun memberWithdrawConfig() {
        val memberWithdrawConfig =
            manager.memberWithdrawConfig(AccountManager.getToken(), ViewModelSubscribeListener(withdrawConfigLiveData))
        dispose.add(memberWithdrawConfig)
    }

    fun memberWithdraw(money: String, type: String, withdrawToken: String) {
        val listener = ViewModelSubscribeListener(withdrawLiveData)
        val withdraw = manager.memberWithdraw(AccountManager.getToken(), money, type, withdrawToken, listener)
        dispose.add(withdraw)
    }

    /**
     * 支付结果
     */
    fun payResultSubscribe(): MutableLiveData<PayResult> {
        val payResult = RxBus.toObservable(PayResult::class.java)
            .subscribeOn(AndroidSchedulers.mainThread()).subscribe({
                payResultLiveData.value = it
            }, {
                it.printStackTrace()
                payResultLiveData.value = PayResult(false, it.message ?: "")
            })
        dispose.add(payResult)
        return payResultLiveData
    }


    /**
     * 微信授权结果观察
     */
    fun weChatBindSubscribe(): MutableLiveData<ThirdLoginEvent> {
        val subscribe = RxBus.toObservable(ThirdLoginEvent::class.java).observeOn(AndroidSchedulers.mainThread())
            .subscribe({ weChatResultLiveData.value = it }, { it.printStackTrace() })
        dispose.add(subscribe)
        return weChatResultLiveData
    }

    fun getRechargeList() {
        val listener = ViewModelSubscribeListener(rechargeListLiveData)
        val withdraw = manager.getRechargeList(listener)
        dispose.add(withdraw)
    }


    fun getRecharge() {
        val listener = ViewModelSubscribeListener(recharge2LiveData)
        val recharge = manager.getRecharge(listener)
        dispose.add(recharge)
    }




}